package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.FileParser;
import model.Manuscript;
import model.State;
import model.User;

/** AssignSPC Panel is the panel that allows a Program Chair to select a SPC
 * @author Sarah George and Kevin Anderson
 *
 */
@SuppressWarnings("serial")
public class AssignSPCPanel extends JPanel {
	
	private State my_state;
	private JFrame my_frame;
	private User[] my_list_of_poss_spcs;
	private JComboBox<User> my_user_comboBox;
	private Manuscript my_paper;
	
	public AssignSPCPanel(State the_state, JFrame the_frame, User[] the_users, Manuscript the_paper) {
		super();
		my_state = the_state;
		my_frame = the_frame;
		my_list_of_poss_spcs = the_users;
		my_paper = the_paper;
		setup();
	}
	
	public void setup() {
		GridLayout exp_layout = new GridLayout(0,2);
		this.setLayout(exp_layout);
		
		this.add(new JLabel("Choose a Subprogram Chair: "));
		setupBox();
		this.add(my_user_comboBox);
		
		CloseAction cancel = new CloseAction(my_frame);
		JButton cancel_add = new JButton(cancel);
		cancel_add.setText("Cancel");
		
		
		this.add(cancel_add);
		
		//ONLY ALLOW ASSIGN IF THERE ARE OPTIONS
		if(my_user_comboBox.getItemCount() > 0)
			this.add(new JButton(new AssignAction()));
	}

	public void setupBox() {
		
		my_user_comboBox = new JComboBox<User>();
		
		for (int i = 0; i < my_list_of_poss_spcs.length; i++) {
			//DO NOT ALLOW SPCS TO BE ASSIGNED THEIR OWN PAPER.
			//DO NOT ALLOW SPCS TO BE ASSIGNED MORE THAN 4 PAPERS
			int spcCount = my_state.getParser().getAssignCountForSPC(my_state.getConference().getConfID(),
							my_list_of_poss_spcs[i].getID());
			if (my_paper.getMy_author() != my_list_of_poss_spcs[i].getID() &&
					spcCount < 4)
				my_user_comboBox.addItem(my_list_of_poss_spcs[i]);
		}
		
		//NOTIFY THE USER IF THE DROPDOWN IS BLANK.
		if(my_user_comboBox.getItemCount() == 0)
		{
			JOptionPane.showMessageDialog(this, "There are no valid SPCs to assign");
			my_frame.dispose();
		}
		my_user_comboBox.setVisible(true);
		this.validate();
	}
	
	private class AssignAction extends AbstractAction
	{
		public AssignAction() {
			super("Assign");
		    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
		    putValue(Action.SELECTED_KEY, true);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			FileParser fp = my_state.getParser();
			if(fp.setSPC(my_paper,((User) my_user_comboBox.getSelectedItem()).getID()))
			{
				JOptionPane.showMessageDialog(AssignSPCPanel.this, "You Successfully assigned the SPC.");
			} else {
				JOptionPane.showMessageDialog(AssignSPCPanel.this, "The SPC was not assigned.");
			}
			
			my_frame.dispose();
			//if spc chosen is listed as a reviewer in a different conf, set role as spc if chosen
		}

	}

}
